package figure8;

//To decrease sensitivity you can add more diff(erence)s or
//make it less accurate by dividing System.currentTimeMillis
//and the return value by a larger exponent of 10
public class TapTimer {
	private long diff0, diff1, diff2, diff3, diff4, 
	             diff5, timeLast, sumOfDiff; 
	private int  amountOfDiffs;

	//initialise values
	public TapTimer() {
		timeLast = System.currentTimeMillis();
		diff0 = diff1 = diff2 = diff3 =
		diff4 = diff5 = sumOfDiff = 0;
		amountOfDiffs = 6;
	}	

	//update values
	//returns current bpm as long type
	public long Update() {
		//get rid of oldest value
		sumOfDiff = sumOfDiff - diff5;
		
		//shift values along
		diff5 = diff4; diff4 = diff3; diff3 = diff2;
		diff2 = diff1; diff1 = diff0;
		
		//get new value
		diff0 = (System.currentTimeMillis()) - timeLast;
		timeLast = (System.currentTimeMillis());
		sumOfDiff = sumOfDiff + diff0;
		
		//return the current average bpm
		return ((amountOfDiffs)*60000/((sumOfDiff)));
	}

}